package com.ms.controller;
import com.ms.common.Result;
import com.ms.pojo.Admin;
import com.ms.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员接口")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录接口")
    public Result<Admin> login(@Valid @RequestBody Admin admin) {
        // 参数校验
        if (admin.getAdminUsername() == null || admin.getAdminUsername().trim().isEmpty()) {
            return Result.error("用户名不能为空");
        }
        if (admin.getAdminPassword() == null || admin.getAdminPassword().trim().isEmpty()) {
            return Result.error("密码不能为空");
        }

        Admin adminLogin = adminService.adminlogin(admin.getAdminUsername(), admin.getAdminPassword());
        if (adminLogin != null) {
            // 登录成功，返回用户信息
            return Result.success(adminLogin);
        } else {
            // 登录失败，返回错误信息
            return Result.error("用户名或密码错误");
        }
    }
}

